/*
 * Copyright 2009 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gtugs.service;

import org.gtugs.domain.Event;
import org.gtugs.domain.Point;
import org.springframework.validation.Validator;
import org.springframework.validation.Errors;

import java.net.MalformedURLException;
import java.net.URL;

/**
 * @author jasonacooper@google.com (Jason Cooper)
 */
public class EventValidator implements Validator {

  private MapsService mapsService;

  public boolean supports(Class c) {
    return Event.class.equals(c);
  }

  public void validate(Object obj, Errors errors) {
    Event event = (Event) obj;
    boolean locationErrors = false;

    if (event == null) {
      errors.rejectValue("name", null, null, "Name required");
    }
    else {
      if (event.getName() == null || event.getName().equals("")) {
        errors.rejectValue("name", null, null, "Name required");
      }
      if (event.getTimeZone() == null || event.getTimeZone().equals("")) {
        errors.rejectValue("timeZone", null, null, "Time zone required");
      }
      if (event.getStartDate() == null) {
        errors.rejectValue("startDate", null, null, "Start date required");
      } else if (event.getEndDate() == null) {
        errors.rejectValue("endDate", null, null, "End date required");
      } else if (event.getEndDate().before(event.getStartDate())) {
        errors.rejectValue("endDate", null, null,
            "End time must occur after start time");
      }
      if (event.getStreetAddress() == null ||
          event.getStreetAddress().equals("")) {
        errors.rejectValue("streetAddress", null, null, "Address required");
        locationErrors = true;
      }
      if (event.getCity() == null || event.getCity().equals("")) {
        errors.rejectValue("city", null, null, "City required");
        locationErrors = true;
      }
      if (event.getCountry() == null || event.getCountry().equals("")) {
        errors.rejectValue("country", null, null, "Country required");
        locationErrors = true;
      }
      if (event.getUrl() != null && !event.getUrl().equals("") &&
          !isValidUrl(event.getUrl())) {
        errors.rejectValue("url", null, null, "Invalid URL");
      }
      if (event.getDescription() == null ||
          event.getDescription().equals("")) {
        errors.rejectValue("description", null, null, "Description required");
      }

      if (!locationErrors) {
        Point point = mapsService.getCoordinates(event.getStreetAddress(),
            event.getCity(), event.getState(), event.getCountry());
        if (point == null) {
          errors.rejectValue("streetAddress", null, null, "Geocoding failed: given address may be invalid or geocoding service may be offline");
          errors.rejectValue("city", null, null, "Geocoding failed: given address may be invalid or geocoding service may be offline");
          errors.rejectValue("state", null, null, "Geocoding failed: given address may be invalid or geocoding service may be offline");
          errors.rejectValue("country", null, null, "Geocoding failed: given address may be invalid or geocoding service may be offline");
        } else {
          event.setLatitude(point.getLatitude());
          event.setLongitude(point.getLongitude());
        }
      }
    }
  }

  private boolean isValidUrl(String urlString) {
    try {
      URL url = new URL(urlString);
    } catch (MalformedURLException e) {
      return false;
    }

    return true;
  }

  public void setMapsService(MapsService mapsService) {
    this.mapsService = mapsService;
  }
}
